
package processors;

import containers.Groups;
import containers.TestsTaken;
import java.util.LinkedList;
import readers.QueryStringHandler;
import readers.UserReader;

/**
 * Bad name class choice, sorry this changed with time
 * This class now handles processing of all quizzes
 * @author Chris
 */
public class processorNextAvailable extends processor {
    private String _errMsg;
    processorNextAvailable(String query) {
        super(query);
        _errMsg = "[ERROR]";
    }
    
    @Override
    String process() {
        QueryStringHandler qsh = new QueryStringHandler(_queryString);
        String userName = qsh.getElement("username");
        
        if(userName == null) {
            return _errMsg;
        }
        LinkedList<Groups> grps = new LinkedList<Groups>();
        LinkedList<String> tests = new LinkedList<String>();
        LinkedList<TestsTaken> taken = new LinkedList<TestsTaken>();
        //Basically a place to store the final list of all the tests combined
        //with their test scores
        LinkedList<TestsTaken> finalList = new LinkedList<TestsTaken>();
        
        //This will get all available groups to the student
        if(!UserReader.getStudentsGroups(userName, grps)) {
            grps.clear();
            return _errMsg;
        }

        String result = "";
        
        TestsTaken tt = new TestsTaken();
        Groups grp;
        
        while(!grps.isEmpty()) {
            grp = grps.removeFirst();
            UserReader.getTests(grp.teacherName, grp.groupName, tests);
            while(!tests.isEmpty()) {
                tt = new TestsTaken();
                tt.group = grp.groupName;
                tt.score = 0;
                tt.teacher = grp.teacherName;
                tt.test = tests.removeFirst();
                finalList.add(tt);
            }
        }
        
        String str = "";
        UserReader.getTakenTests(userName, taken);
        
        while(!finalList.isEmpty()) {
            tt = finalList.removeFirst();
            //str += taken.contains(tt) + tt.teacher+tt.group+tt.test+"\n";
            
            if(!taken.contains(tt)) {
                return tt.teacher + "_" + tt.group + " " + tt.test;
            }
        }
        
        return str;
        //return "NOTEST";
    }
}
